package com.example.test.controller;

import com.example.test.entity.Product;
import com.example.test.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/api/products")
@CrossOrigin
public class ProductController {

    @Autowired
    private ProductService productService;

    @PostMapping("/upload")
    public ResponseEntity<Product> uploadProduct(@RequestParam("name") String name,
                                                 @RequestParam("description") String description,
                                                 @RequestParam("quantity") int quantity,
                                                 @RequestParam("price") double price,
                                                 @RequestParam("ifground") int ifground,
                                                 @RequestParam("image") MultipartFile image,
                                                 @RequestParam("username") String username) throws IOException {
        Product product = new Product();
        product.setName(name);
        product.setDescription(description);
        product.setQuantity(quantity);
        product.setPrice(price);
        product.setIfground(ifground);
        product.setSellerUsername(username); // 设置卖方用户名信息
        return ResponseEntity.ok(productService.saveProduct(product, image, username));
    }

    @GetMapping
    public ResponseEntity<List<Product>> getAllProducts() {
        return ResponseEntity.ok(productService.getAllProducts());
    }

    @GetMapping("/check")
    public ResponseEntity<List<Product>> checkProduct() {
        return ResponseEntity.ok(productService.checkProduct());
    }
    @PutMapping("/{id}")
    public ResponseEntity<Product> updateProduct(@PathVariable Long id,
                                                 @RequestParam("name") String name,
                                                 @RequestParam("description") String description,
                                                 @RequestParam("quantity") int quantity,
                                                 @RequestParam("price") double price,
                                                 @RequestParam("ifground") int ifground,
                                                 @RequestParam(value = "image", required = false) MultipartFile image,
                                                 @RequestParam("username") String username) throws IOException {
        Product product = new Product();
        product.setName(name);
        product.setDescription(description);
        product.setQuantity(quantity);
        product.setPrice(price);
        product.setIfground(ifground);
        product.setSellerUsername(username); // 设置卖方用户名信息
        return ResponseEntity.ok(productService.updateProduct(id, product, image, username));
    }


    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteProduct(@PathVariable Long id) {
        productService.deleteProduct(id);
        return ResponseEntity.noContent().build();
    }
    @PostMapping("/{id}")
    public ResponseEntity<Product> getProductById(@PathVariable Long id) {
      return ResponseEntity.ok(productService.queryById(id));
    }
    @PutMapping("/test")
    public ResponseEntity<Void>updateQuantity(@RequestBody List<Product>products){
        productService.updateQuantity(products);
        return ResponseEntity.noContent().build();
    }
}
